package model;

import constant.MapElements;
import frame.GamePanel;
import type.Buff;
import type.GameMode;
import static constant.Probabilities.*;
import static frame.MainFrame.GAME_MODE;

public class Food {
    public GamePanel panel;    // 所属游戏面板
    public int row, col;      // 坐标
    public Buff buff;      // 所带加成 buff
    public boolean isLive; // 是否存活
    public int value;      // 食物分值

    public Food(GamePanel panel, int row, int col, Buff buff) {
        this.panel = panel;
        this.row = row;
        this.col = col;
        this.buff = buff;

        // 根据 buff 赋予分值
        switch (buff) {
            case NORMAL:
            case SHIELD:
            case THROUGH_WALL:
                value = 1;
                break;
            case SPEED_UP:
                value = 5;
                break;
        }

        // panel 中 map 数组的变化
        panel.map[row][col] = MapElements.FOOD;
        isLive = true; // 默认存活

    }

    public static Food createFood(GamePanel panel, int row, int col,int length) {
        if (GAME_MODE == GameMode.CLASSIC) {
            return new Food(panel, row, col, Buff.NORMAL); // 经典模式只给普通食物
        } else {
            if (length >= 4 && length < 8) {
                if (NORMAL_FOOD_PROB < 1 && NORMAL_FOOD_PROB >= 0.1 && !panel.firstChange){
                    NORMAL_FOOD_PROB -= 0.1;
                    panel.firstChange = true;
                    System.out.println("概率第1次出现变化");
                    System.out.println("普通食物概率" + NORMAL_FOOD_PROB + "加速食物概率"+SPEED_UP_FOOD_PROB+
                            "护盾食物概率"+SHIELD_FOOD_PROB+"穿墙食物概率"+THROUGH_WALL_FOOD_PROB);
                }
                switch (randomProbability(NORMAL_FOOD_PROB,SPEED_UP_FOOD_PROB,
                        SHIELD_FOOD_PROB,THROUGH_WALL_FOOD_PROB)){
                    case 1:
                        return new Food(panel, row, col, Buff.NORMAL);
                    case 2:
                        return new Food(panel, row, col, Buff.SPEED_UP);
                    case 3:
                        return new Food(panel, row, col, Buff.SHIELD);
                    case 4:
                        return new Food(panel, row, col, Buff.THROUGH_WALL);
                    default:
                        System.out.println("概率设置有误！！！");
                        break;
                }
            } else if (length >= 8 && length < 15) {
                if (NORMAL_FOOD_PROB < 1 && NORMAL_FOOD_PROB >= 0.2 && !panel.secondChange){
                    NORMAL_FOOD_PROB -= 0.1;
                    panel.secondChange = true;
                    System.out.println("概率第2次出现变化");
                    System.out.println("普通食物概率"+NORMAL_FOOD_PROB+"加速食物概率"+SPEED_UP_FOOD_PROB+
                            "护盾食物概率"+SHIELD_FOOD_PROB+"穿墙食物概率"+THROUGH_WALL_FOOD_PROB);
                }
                switch (randomProbability(NORMAL_FOOD_PROB,SPEED_UP_FOOD_PROB,
                        SHIELD_FOOD_PROB,THROUGH_WALL_FOOD_PROB)){
                    case 1:
                        return new Food(panel, row, col, Buff.NORMAL);
                    case 2:
                        return new Food(panel, row, col, Buff.SPEED_UP);
                    case 3:
                        return new Food(panel, row, col, Buff.SHIELD);
                    case 4:
                        return new Food(panel, row, col, Buff.THROUGH_WALL);
                    default:
                        System.out.println("概率设置有误！！！");
                        break;
                }

            }else if (length >= 15) {
                if (NORMAL_FOOD_PROB < 1 && NORMAL_FOOD_PROB >= 0.2 && !panel.thirdChange){
                    NORMAL_FOOD_PROB -= 0.2;
                    panel.thirdChange = true;
                    System.out.println("概率第3次出现变化");
                    System.out.println("普通食物概率"+NORMAL_FOOD_PROB+"加速食物概率"+SPEED_UP_FOOD_PROB+
                            "护盾食物概率"+SHIELD_FOOD_PROB+"穿墙食物概率"+THROUGH_WALL_FOOD_PROB);
                }
                switch (randomProbability(NORMAL_FOOD_PROB,SPEED_UP_FOOD_PROB,
                        SHIELD_FOOD_PROB,THROUGH_WALL_FOOD_PROB)){
                    case 1:
                        return new Food(panel, row, col, Buff.NORMAL);
                    case 2:
                        return new Food(panel, row, col, Buff.SPEED_UP);
                    case 3:
                        return new Food(panel, row, col, Buff.SHIELD);
                    case 4:
                        return new Food(panel, row, col, Buff.THROUGH_WALL);
                    default:
                        System.out.println("概率设置有误！！！");
                        break;
                }
            } else {
                // 其他长度只给普通食物
                System.out.println("普通食物概率"+NORMAL_FOOD_PROB+"加速食物概率"+SPEED_UP_FOOD_PROB+
                        "护盾食物概率"+SHIELD_FOOD_PROB+"穿墙食物概率"+THROUGH_WALL_FOOD_PROB);
                return new Food(panel, row, col, Buff.NORMAL);
            }
        }
        return null;
    }
}
